//
//  PlaceInfViewController.m
//  Trip
//
//  Created by dlios on 15-7-14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PlaceInfViewController.h"
#import "GongLueCollectionViewCell.h"
#import "MyCollectionReusableView.h"
#import "PlaceInfInfViewController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworkHandler.h"

@interface PlaceInfViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation PlaceInfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self headlerData];
    
    
}

- (void)headlerData
{
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/%@.json?page=1", self.str];
    [AFNetworkHandler GetWithAFNByURL:url completion:^(id result) {
       
        self.souArr = result;
        [self.collectionView reloadData];
//        NSLog(@"%@", self.souArr);
        [self setCollectionView];
    }];
}

- (void)setCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width / 2 - 20, 25);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 30);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"gailun"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"chuxing"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"daoda"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"jiaotong"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"yule"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"meishi"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"gouwu"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"qita"];
    [self.collectionView registerClass:[GongLueCollectionViewCell class] forCellWithReuseIdentifier:@"qqq"];
    
    [self.collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    NSArray *arr = @[@"概论", @"出行须知", @"如何到达", @"当地交通", @"娱乐", @"美食", @"购物", @"其他"];
//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        MyCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        reuseView.myLabel.text = [arr objectAtIndex:indexPath.section];
//        reuseView.myLabel.textAlignment = NSTextAlignmentLeft;
//        return reuseView;
//    }
//    return nil;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.souArr.count == 0) {
        return 0;
    }
    if (section == 0) {
        return [[[self.souArr objectAtIndex:section] objectForKey:@"pages"] count];
    }
    if (section == 1) {
        return [[[self.souArr objectAtIndex:section] objectForKey:@"pages"] count];
    }
    if (section == 2) {
        return [[[self.souArr objectAtIndex:section] objectForKey:@"pages"] count];
    }
    if (section == 3) {
        return [[[self.souArr objectAtIndex:section] objectForKey:@"pages"] count];
    }
    else return 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"gailun" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 1) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"chuxing" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 2) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"daoda" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 3) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"jiaotong" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 4) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"yule" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 5) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"meishi" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 6) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"gouwu" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
    if (indexPath.section == 7) {
        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"qita" forIndexPath:indexPath];
        gailun.label.text = [[[[self.souArr objectAtIndex:indexPath.section] objectForKey:@"pages"] objectAtIndex:indexPath.item] objectForKey:@"title"];
        return gailun;
    }
//    if (indexPath.section == 8) {
//        GongLueCollectionViewCell *gailun = [collectionView dequeueReusableCellWithReuseIdentifier:@"qqq" forIndexPath:indexPath];
//        return gailun;
//    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PlaceInfInfViewController *placeVC = [[PlaceInfInfViewController alloc] init];
    [self.navigationController pushViewController:placeVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
